/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts.controleur;

import com.myapp.struts.modele.Etudiant;
import com.myapp.struts.modele.QueryBD;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.PageContext;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.DynaProperty;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author Izii Tom's
 */
public class AppelAction extends Action {

    private Hashtable<String, String> resultats;
    private ArrayList<HashMap<String, Object>> res;

    /**
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        DynaBean dynaForm = (DynaBean) form;
        res = convertDynaBeanListToArrayList((List) dynaForm.get("bdEtu1"));
        QueryBD qb = (QueryBD) request.getSession().getAttribute("bd");

        System.out.println(qb.getCours().getNcours());
        System.out.println(qb.getMatiere().getIdmatiere());

        for (int i = 0; i < res.size(); i++) {
            try {
                if (((String) res.get(i).get("etat")).equalsIgnoreCase("p")) {
                    qb.appelPresent((String) res.get(i).get("nom"), qb.getCours().getNcours());
                }
                if (((String) res.get(i).get("etat")).equalsIgnoreCase("a")) {
                    qb.appelAbsent((String) res.get(i).get("nom"), qb.getCours().getNcours() , qb.getMatiere().getIdmatiere());
                }
                if (((String) res.get(i).get("etat")).equalsIgnoreCase("e")) {
                    qb.appelExcuse((String) res.get(i).get("nom"), qb.getCours().getNcours());
                }
                /*if(!(((String) res.get(i).get("etat")).equalsIgnoreCase("p")) || !(((String) res.get(i).get("etat")).equalsIgnoreCase("a")) || !(((String) res.get(i).get("etat")).equalsIgnoreCase("e")) || ((String) res.get(i).get("etat"))=="" ){
                    errors.add("etat", new ActionMessage("errors.appel.required"));
                }*/

            } catch (Exception ex) {
                System.out.println(ex);
            }
            
        }

        return mapping.findForward("enseignant");   
        
    }

    public ArrayList<HashMap<String, Object>> convertDynaBeanListToArrayList(List<DynaBean> theList) {
        System.out.println("the list : " + theList);
        ArrayList<HashMap<String, Object>> result = new ArrayList<HashMap<String, Object>>();
        DynaProperty[] dynaProperties = null;
        for (Integer i = 0; i < theList.size(); i++) {
            DynaBean row = theList.get(i);
            HashMap<String, Object> resultRow
                    = new HashMap<String, Object>();

            if (dynaProperties == null) {
                dynaProperties = row.getDynaClass().getDynaProperties();
            }
            for (Integer j = 0; j < dynaProperties.length; j++) {
                String columnName = dynaProperties[j].getName();

//                System.out.println(columnName);
                resultRow.put(columnName, row.get(columnName));
            }
            result.add(resultRow);
        }

        return result;
    }

}
